package com.example.presentshopping.ui.fragment

import BaseRecyclerAdapter
import android.os.Bundle
import android.view.View
import android.widget.ListView
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.example.presentshopping.R
import com.example.presentshopping.config.base.BaseMvpFragment
import com.example.presentshopping.config.bean.CategoryListBean
import com.example.presentshopping.mvp.presenter.PublicPresenter
import com.example.presentshopping.net.http.HttpRequest
import com.example.presentshopping.net.http.HttpURL
import com.example.presentshopping.net.observer.TaskCallback
import com.example.presentshopping.ui.adapter.MyFragmentAdapter
import com.example.presentshopping.ui.adapter.MyProvinceAdapter
import com.example.presentshopping.utils.tool.GsonUtils
import java.util.*

class ClientFragment :  BaseMvpFragment() {


    var categoryListBean : CategoryListBean?=null
    var  categoryL : CategoryListBean.DataBean.ListBean ?=null
    var sendMesPresenter: PublicPresenter? = null

    var vp_classify: ViewPager? = null

    private var AreaAdapter: MyProvinceAdapter? = null

    var baseRecyclerAdapter: BaseRecyclerAdapter<CategoryListBean.DataBean.ListBean>? = null

    var  rv_title : ListView?=null
    private var fragmentList: List<Fragment>? = null

    override val layoutId: Int
        get() = R.layout.fragment_client

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        sendMesPresenter = PublicPresenter(context, false, "")
        sendMesPresenter?.attachView(this)
        rv_title = view?.findViewById(R.id.rv_title)
        vp_classify =view?.findViewById(R.id.vp_classify)
        GoodsCategoryList()
    }

    fun GoodsCategoryList(){
        sendMesPresenter?.netWorkRequestPost(
            HttpURL.Req_getGoodsCategoryList,
            HttpRequest.getGoodsDetail(""), object : TaskCallback {
                override fun onSuccess(data: String?) {
                    categoryListBean =
                        GsonUtils.getGson().fromJson(data, CategoryListBean().javaClass)
                    AreaAdapter = MyProvinceAdapter(categoryListBean?.data?.list, context)

                    fragmentList = ArrayList()
                    for (categoryL in categoryListBean?.data?.list!!) {
                        val localStoreFragment: Fragment =
                            YDClassifyItemFragment(categoryL)
                        (fragmentList as ArrayList<Fragment>).add(localStoreFragment)
                    }

                    vp_classify!!.offscreenPageLimit = 1
                    vp_classify!!.adapter = MyFragmentAdapter(childFragmentManager, fragmentList)
                    vp_classify!!.currentItem = 0
                    //    vp_classify!!.addOnPageChangeListener(classifyTitleAdapter)

                    rv_title?.adapter = this@ClientFragment.AreaAdapter
                    AreaAdapter?.setOnShengClickListener { view, position, mp, mpList ->
                        AreaAdapter?.setGrouPosition(position)
                        vp_classify!!.currentItem = position
                        AreaAdapter!!.notifyDataSetChanged()
                    }


                }

                override fun onFailure(
                    e: Throwable?,
                    code: Int,
                    msg: String?,
                    isNetWorkError: Boolean
                ) {

                }

            })
    }

    override fun onDestroyView() {
        sendMesPresenter?.detachView()
        super.onDestroyView()
    }
    override fun onSuccess(data: String?) {

    }

    override fun onFailure(e: Throwable?, code: Int, msg: String?, isNetWorkError: Boolean) {

    }
}